<template>
    <div class="pop-selectitem-list">
        <el-dialog title="选择产品参与活动" :visible.sync="dialogVisible" top="5vh" width="70%" :close-on-click-modal="false">
            <div class="search-box" v-show="!is_bill">
                <div class="search-box-select">
                    <el-form :model="searchform">
                        <el-form-item>
                            <el-cascader v-model="searchform.catid" :options="cat_ls" class="sele-w" :props="propsdata"
                                @change="changeList"></el-cascader>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="searchform.storeid" class="sele-w" popper-class="method-box"
                                @change="changeList">
                                <el-option v-for="item in qty_ls_data" :key="item.id" :label="item.name"
                                    :value="item.id">
                                    <div class="option-title">
                                        {{ item.name }}
                                    </div>
                                    <div class="option-doc">{{ item.num }}</div>
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-select v-model="searchform.is_stock" @change="changeList">
                                <el-option v-for="item in godd_status_ls" :key="item.id" :label="item.name"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="list-box" v-infinite-scroll="loadFun">
                    <div class="list" v-if="list_data.length > 0">
                        <div class="list-row" v-for="item in list_data" :key="item.id" @click="chegoodFun(item)">
                            <div class="list-info">
                                <div class="img-box">
                                    <img :src="item.imgs ? item.imgs : img_buf" alt="" />
                                </div>
                                <div class="info-box">
                                    <div class="title">
                                        <span>{{ item.title }}</span>
                                    </div>
                                    <div class="phone">
                                        <span>库存：{{ item.qty }}</span>
                                    </div>
                                    <div class="tiem">
                                        <span>{{ item.price }}</span>
                                        <span class="f-num" v-if="item.total_num > 0">{{ item.total_num }}</span>
                                    </div>
                                    <div class="tag-box">
                                        <span v-for="(tag_item, tag_index) in item.tagsInfo" 
                                            :key="tag_index" 
                                            :style="{ backgroundColor: color_data[tag_index],}">
                                            {{ tag_item }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <empty v-else />
                </div>
                <span slot="footer" class="dialog-footer">
                    <div class="num">
                        <span>数量：{{ totalNum }}</span><span>总额：{{ totalpic }}</span>
                    </div>
                    <div class="but">
                        <el-button @click="cancelFun">取 消</el-button>
                        <el-button type="primary" @click="determineFun" v-show="!is_bill">选 好 了</el-button>
                    </div>
                </span>
            </div>
            <popGoodFilling ref="popGoodFilling" v-show="is_bill" @cancelFun="cancelFun"
                @determineFilling="determineFilling"></popGoodFilling>
        </el-dialog>
    </div>
</template>

<script>
import empty from "../comm/empty.vue";
import config from "@/js/conf/Config.js";
import staticConst from "@/js/const/StaticConst.js";
import popGoodFilling from "./popGoodFilling.vue";

export default {
    components: { empty, popGoodFilling },
    data() {
        return {
            img_buf: staticConst.GOOD_IMG,
            is_bill: false,
            is_soll: false,
            dialogVisible: false,
            searchform: {
                catid: "",
                pfrom: 0,
                pnum: 20,
                page: "1",
                idyun: "",
                storeid: 0,
                is_stock: "",
            },
            cat_ls: [],
            qty_ls_data: [],
            list_data: [], //展示的产品列表
            list_data_back: [], //最终选好的产品列表
            edit_spec_data: [],
            totalNum: 0,
            totalpic: 0,
            propsdata: {
                children: "son",
                label: "title",
                value: "id",
                expandTrigger: "hover",
                checkStrictly: true,
            },
            color_data: [
                "#3989fc",
                "#ee464b",
                "#19c56b",
                "#fd641c",
                "#3989fc",
                "#ee464b",
            ],
            godd_status_ls: [
                {
                    id: "",
                    name: "全部产品",
                },
                {
                    id: "1",
                    name: "有货产品",
                },
                {
                    id: "2",
                    name: "无货产品",
                },
                {
                    id: "3",
                    name: "上架产品",
                },
                {
                    id: "4",
                    name: "下架产品",
                },
                {
                    id: "5",
                    name: "已选产品",
                },
            ],
        };
    },
    methods: {
        async initData(data) {
            this.list_data_back = [];
            this.totalNum = 0;
            this.totalpic = 0;
            this.searchform.idyun = data.shopid_yd;
            // this.dialogVisible = true; // 显示弹窗
            this.emptyData();
            await this.getlistdata();
            if (this.cat_ls.length == 0) {
                await this.getCatListFun();
            }
        },
        editSkuinfo(data) {
            this.dialogVisible = true;
            this.is_bill = true;
            this.$refs.popGoodFilling.editSkuinfo(data);
        },
        // 接收编辑的sku数据
        determineFilling(data) {
            this.totalNum = 0;
            this.totalpic = 0;
            this.is_bill = false;
            this.edit_spec_data.push(data);
            this.list_data.map((v) => {
                if (v.id == data.id) {
                    v.total_num = data.total_num;
                    v.total_pic = data.total_pic;
                    v.spec_data = data.spec_data;
                }
            });
            this.list_data_back.map((v) => {
                if (v.id == data.id) {
                    v.total_num = data.total_num;
                    v.total_pic = data.total_pic;
                    v.spec_data = data.spec_data;
                }
                this.totalNum += v.total_num;
                this.totalpic += v.total_pic;
            });
        },
        // 筛选
        changeList() {
            if (this.searchform.is_stock == 5) {
                this.emptyData();
            } else {
                this.emptyData();
                this.getlistdata();
            }
        },
        // 点击产品
        chegoodFun(item) {
            this.is_bill = true;
            this.$refs.popGoodFilling.initData(item);
        },
        // 滚动加载
        loadFun() {
            if (this.is_soll) return;
            this.searchform.pfrom += 20;
            this.getlistdata();
        },
        
        // 选好了(组装sku列表数据)
        determineFun() {
            let gd_ls = [];
            this.list_data_back.map((v) => {
                if (v.total_num > 0) {
                    v.spec_data.map((vs) => {
                        if (vs.list) {
                            vs.list.map((vw) => {
                                if (vw.list) {
                                    vw.list.map((vk) => {
                                        vk.unit_ls.map((vu) => {
                                            if (vu.number > 0) {
                                                gd_ls.push(vu);
                                            }
                                        });
                                    });
                                } else {
                                    vw.unit_ls.map((vu) => {
                                        if (vu.number > 0) {
                                            gd_ls.push(vu);
                                        }
                                    });
                                }
                            });
                        } else {
                            vs.unit_ls.map((vu) => {
                                if (vu.number > 0) {
                                    gd_ls.push(vu);
                                }
                            });
                        }
                    });
                }
            });
            gd_ls.map((v) => {
                this.list_data_back.map((vs) => {
                    if (v.gdid == vs.id) {
                        v.spec_data = JSON.parse(JSON.stringify(vs.spec_data));
                    }
                });
            });
            this.dialogVisible = false;
            this.$emit("determineFun", gd_ls);
        },
        // 取消
        cancelFun() {
            if (this.is_bill) {
                this.is_bill = false;
            } else {
                this.dialogVisible = false;
            }
        },

        // 获取产品列表
        async getlistdata() {
            this.is_soll = true;
            let url = config.GOODS_LIST;
            let param = Object.assign({}, this.searchform);
            param.catid = param.catid
                ? param.catid[param.catid.length - 1]
                : "";
            let res = await this.$api({ url: url, data: param });
            if (res.status == 1) {
                if (res.data.store_ls && this.qty_ls_data.length == 0) {
                    this.qty_ls_data = res.data.store_ls;
                }
                if (res.data.goodsInfo) {
                    res.data.goodsInfo.map((v) => {
                        v.total_num = 0;
                        v.total_pic = 0;
                        if (v.tagsInfo) {
                            v.tagsInfo.unshift("上架");
                        } else {
                            v.tagsInfo = ["上架"];
                        }
                        if (this.edit_spec_data.length > 0) {
                            this.edit_spec_data.map((vs) => {
                                if (v.id == vs.id) {
                                    v.spec_data = vs.spec_data;
                                    v.total_num = vs.total_num;
                                    v.total_pic = vs.total_pic;
                                }
                            });
                        }
                    });
                    this.list_data = this.list_data.concat(res.data.goodsInfo);
                    this.list_data_back = this.list_data_back.concat(
                        res.data.goodsInfo
                    );
                    let map = new Map();
                    for (let item of this.list_data_back) {
                        map.set(item.id, item);
                    }
                    this.list_data_back = [...map.values()];
                    setTimeout((v) => {
                        this.is_soll = false;
                    }, 200);
                } else {
                    this.is_soll = true;
                }
            }
        },
        // 获取分类列表
        async getCatListFun() {
            let url = config.GDPRE_CAT_LS;
            let param = {};
            let res = await this.$api({ url: url, data: param });
            if (res.status == 1 && res.data) {
                res.data.unshift({
                    id: "all",
                    title: "全部分类",
                });
                this.searchform.catid = "all";
                this.cat_ls = res.data;
            }
        },
        //清空数据
        emptyData() {
            this.searchform.pfrom = 0;
            this.list_data = [];
            this.edit_spec_data = [];
            this.is_soll = false;
            this.is_bill = false;
        },
    },
};
</script>

<style lang="scss" scoped>
.pop-selectitem-list {
    ::v-deep.search-box-select {
        .el-form {
            display: flex;
        }

        .sele-w {
            margin-right: 10px;
        }
    }
}

.dialog-footer {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .num {
        font-size: 14px;

        span {
            margin-right: 10px;
        }
    }
}

.list-box {
    height: 520px;
    overflow: auto;

    .list-row {
        padding: 10px 0;
        border-bottom: 1px #f2f2f2 solid;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-right: 30px;
        cursor: pointer;

        .list-info {
            display: flex;
            width: 100%;

            .img-box {
                width: 100px;
                height: 100px;
                margin-right: 10px;
                border-radius: 5px;
                background: #eaeaea;
                overflow: hidden;
                display: flex;
                justify-content: center;
                align-items: center;

                img {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                }

                span {
                    font-size: 16px;
                    font-weight: bold;
                }
            }

            .info-box {
                width: calc(100% - 80px);

                .title {
                    font-size: 14px;
                }

                .phone {
                    font-size: 12px;
                    color: #9c9a9a;
                    margin: 5px 0;

                    span {
                        margin-right: 10px;
                    }
                }

                .tiem {
                    width: 100%;
                    font-size: 12px;
                    color: #fa4848;
                    display: flex;
                    justify-content: space-between;

                    .f-num {
                        padding: 0 20px;
                        background: #3388ff;
                        color: #fff;
                        font-size: 12px;
                        margin-left: 10px;
                    }
                }

                .tag-box {
                    span {
                        border-radius: 3px;
                        background: #3388ff;
                        color: #fff;
                        font-size: 12px;
                        padding: 2px 4px;
                        margin-right: 5px;
                    }
                }
            }
        }

        .list-avitve {
            font-size: 20px;
            color: #3388ff;
        }
    }
}

.list-box::-webkit-scrollbar {
    /*滚动条整体样式*/
    width: 5px;
    /*高宽分别对应横竖滚动条的尺寸*/
    height: 1px;
}

.list-box::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
    background: #ccc; //rgba(0, 0, 0, 0.0);
    scrollbar-arrow-color: red;
}

.list-box::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    -webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0);
    border-radius: 0;
    background: rgba(0, 0, 0, 0);
}
</style>